package main

import (
	"fmt"
	"os"
	"os/signal"
	"runtime"
	"syscall"

	"gitee.com/samhula/gospider/parse"
	"gitee.com/samhula/gospider/utils"

	"gitee.com/samhula/gospider/config"

	kingpin "gopkg.in/alecthomas/kingpin.v2"
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	configFile := kingpin.Flag("proxy", "指定配置文件").Short('p').Default("../etc/proxy.ini").String()
	kingpin.Parse()
	config, err := config.NewConfig(*configFile)
	if err != nil {
		fmt.Printf("配置文件错误：%v \n", err)
		return
	}
	utils.SetProcessName(config.ProcessName)
	parse := parse.NewHTTP(*config)
	defer func() {
		fmt.Printf("parse.Stop")
		parse.Stop()
	}()
	go func() {
		parse.Start(config.IPConfig.Host, config.IPConfig.IP, config.IPConfig.UserAgent)
	}()
	sc := make(chan os.Signal, 1)
	signal.Notify(sc,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGKILL,
		syscall.SIGABRT,
	)
	select {
	case sig := <-sc:
		fmt.Printf("Recevied signal: %v \n", sig.String())
		os.Exit(0)
	}
}
